package com.ymatou.iapi.m.couponservice.testcase;

import java.text.SimpleDateFormat;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.CouponInfo;
import com.ymatou.iapi.m.couponservice.parameter.GetCouponsOfBatchByPagingBean;
import com.ymatou.iapi.m.couponservice.service.GetCouponsOfBatchByPagingCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
/**
*分页获取指定批次下的优惠券列表信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCouponsOfBatchByPaging{
	private static GetCouponsOfBatchByPagingBean getcouponsofbatchbypagingBean;
	private static GetCouponsOfBatchByPagingCall getcouponsofbatchbypagingCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("分页获取指定批次下的优惠券列表信息");
	}

	@Before
	public void caseUp() {
		getcouponsofbatchbypagingBean=new GetCouponsOfBatchByPagingBean();
		getcouponsofbatchbypagingCall=new GetCouponsOfBatchByPagingCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcouponsofbatchbypagingCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCouponsOfBatchByPaging_001() {
		Logger.start(true,"分页获取指定批次下的优惠券列表信息");
		try {
			YmtCoupon coupon = new YmtCouponWapper().selectTop1();
			
			getcouponsofbatchbypagingBean.setBatchId(coupon.getiBatchId());
			getcouponsofbatchbypagingBean.setPageSize(20);
			getcouponsofbatchbypagingBean.setPageIndex(1);
			
			getcouponsofbatchbypagingCall.setData(getcouponsofbatchbypagingBean);
			getcouponsofbatchbypagingCall.callService();
			
			Logger.verifyEquals("0", getcouponsofbatchbypagingCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", getcouponsofbatchbypagingCall.getMessage(), "验证返回Message");

			List<CouponInfo> couponInfos = getcouponsofbatchbypagingCall.getCouponInfos().getCouponInfos();
			
			for (int i=0;i<couponInfos.size();i++) {
				CouponInfo couponInfo = couponInfos.get(i);
				
				// 验证返回数据正确性
				YmtCouponPrivateUserBound ymtCouponPrivateUserBound = new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(couponInfo.getCouponCode()).get(0);
				YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(ymtCouponPrivateUserBound.getiBatchId());
				YmtCouponValue value = new YmtCouponValueWapper().selectByiCouponSettingId(batch.getiCouponSettingId()).get(0);
			
				// 验证返回数据的正确性
				Logger.verifyEquals(ymtCouponPrivateUserBound.getiUserId(), couponInfo.getUserId(), "验证返回UserId");
//				Logger.verifyEquals(ymtCouponPrivateUserBound.getiCouponUsedCount(), couponInfo.getRemainUseTimes(), "验证返回RemainUseTimes");
				
				SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
				Logger.verifyEquals(time.format(ymtCouponPrivateUserBound.getdAddTime()).subSequence(0, 19), couponInfo.getReceiveTime().replace("T", " "), "验证返回RemainUseTimes");
			}	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}